import preplanServer from '../server/preplan/preplanServer'

const uuid = require('uuid/v4')
const {
  util: { getCatalogML }
} = require('../util')

const defaultTemplate =
  [{
    id: uuid(),
    pid: 0,
    name: '物证',
    type: '0',
    alias: '物证',
    suffix: '',
    stream: '',
    comment: '',
    order: '3',
    show: 'true',
    fileType: '',
    fileID: '',
    style: '',
    template: ''
  }, {
    id: uuid(),
    pid: 0,
    name: '书证',
    type: '0',
    alias: '书证',
    suffix: '',
    stream: '',
    comment: '',
    order: '4',
    show: 'true',
    fileType: '',
    fileID: '',
    style: '',
    template: ''
  }, {
    id: uuid(),
    pid: 0,
    name: '证人证言',
    type: '0',
    alias: '证人证言',
    suffix: '',
    stream: '',
    comment: '',
    order: '5',
    show: 'true',
    fileType: '',
    fileID: '',
    style: '',
    template: ''
  }, {
    id: uuid(),
    pid: 0,
    name: '被害人陈述',
    type: '0',
    alias: '被害人陈述',
    suffix: '',
    stream: '',
    comment: '',
    order: '6',
    show: 'true',
    fileType: '',
    fileID: '',
    style: '',
    template: ''
  }, {
    id: uuid(),
    pid: 0,
    name: '犯罪嫌疑人供述与辩解',
    type: '0',
    alias: '犯罪嫌疑人供述与辩解',
    suffix: '',
    stream: '',
    comment: '',
    order: '7',
    show: 'true',
    fileType: '',
    fileID: '',
    style: '',
    template: ''
  }, {
    id: uuid(),
    pid: 0,
    name: '鉴定意见',
    type: '0',
    alias: '鉴定意见',
    suffix: '',
    stream: '',
    comment: '',
    order: '8',
    show: 'true',
    fileType: '',
    fileID: '',
    style: '',
    template: ''
  }, {
    id: uuid(),
    pid: 0,
    name: '勘验、检查、辨认、侦查实验等笔录',
    type: '0',
    alias: '勘验、检查、辨认、侦查实验等笔录',
    suffix: '',
    stream: '',
    comment: '',
    order: '9',
    show: 'true',
    fileType: '',
    fileID: '',
    style: '',
    template: ''
  }, {
    id: uuid(),
    pid: 0,
    name: '视听资料、电子数据',
    type: '0',
    alias: '视听资料、电子数据',
    suffix: '',
    stream: '',
    comment: '',
    order: '10',
    show: 'true',
    fileType: '',
    fileID: '',
    style: '',
    template: ''
  }]

const state = {
  evidenceCatalogTreeData: []
}

/**
 * 初始化（加载初始数据）
 * @returns {Promise<void>}
 */
async function init () {
  // 示证大纲树数据
  state.evidenceCatalogTreeData = (await preplanServer.getEvidenceCatalog()) || []

  if (!state.evidenceCatalogTreeData.length) {
    let appearCourt = require('../pages/primary/store/modules/appearCourt').default.state

    let rootNode = {
      id: uuid(),
      pid: '-1',
      name: appearCourt.caseInfo.AJMC,
      type: 0,
      alias: appearCourt.caseInfo.AJMC,
      suffix: '',
      stream: [],
      comment: '',
      order: 0,
      fileType: '',
      fileID: '',
      style: '',
      template: '',
      children: []
    }

    reset(rootNode)

    state.evidenceCatalogTreeData.push(rootNode)
    await preplanServer.addEvidenceCatalog(JSON.stringify(state.evidenceCatalogTreeData))
  }
}

/**
 * 重置（初始化）
 * @param rootNode
 */
function reset (rootNode) {
  rootNode = rootNode || state.evidenceCatalogTreeData[0]

  rootNode.children.splice(0, rootNode.children.length)

  let templateDatas = JSON.parse(localStorage.getItem('templateData')) || []

  let templateData = templateDatas.find((x) => x.default)

  let data = []
  if (templateData) {
    data = templateData.data
  } else {
    data = JSON.parse(JSON.stringify(defaultTemplate))
  }
  data.forEach((item) => {
    item.pid = rootNode.id
    rootNode.children.push(item)
  })
}

/**
 * 导入模板
 * @param data
 */
function importTemplate (data) {
  data = JSON.parse(JSON.stringify(data))

  let rootNode = state.evidenceCatalogTreeData[0]
  rootNode.children.splice(0, rootNode.children.length)
  data.forEach((item) => {
    item.pid = rootNode.id
    rootNode.children.push(item)
  })
}

/**
 *同步到数据库
 */
async function synch () {
  let result = await preplanServer.saveEvidenceCatalog(JSON.stringify(state.evidenceCatalogTreeData))

  console.log('示证目录更新完成：', result)
}

function _getCatalogML () {
  return getCatalogML(JSON.parse(JSON.stringify(state.evidenceCatalogTreeData)))
}

export default {
  state: state,
  defaultTemplate: defaultTemplate,
  init: init,
  reset: reset,
  importTemplate: importTemplate,
  synch: synch,
  getCatalogML: _getCatalogML
}
